package com.ijava.base.dao.impl;


import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ijava.base.dao.OrganizationDAO;
import com.ijava.base.domain.Organization;

public class OrganizationDAOImpl extends HibernateDaoSupport  implements OrganizationDAO {

	public void save(Organization organization) throws Exception {
		getHibernateTemplate().save(organization);
	}

	public void remove(String id) throws Exception {
		getHibernateTemplate().delete(get(id));
	}

	public void update(Organization organization) throws Exception {
		getHibernateTemplate().update(organization);
	}
	public void merge(Organization organization) throws Exception{
		getHibernateTemplate().merge(organization);
	}
	public void saveOrUpdate(Organization organization) throws Exception {
		getHibernateTemplate().saveOrUpdate(organization);
	}

	public Organization get(String id) throws Exception {
		return getHibernateTemplate().get(Organization.class, id);
	}

	public List findWithPaging(final String queryString,final List paramList, final int firstRow,
			final int maxRow) throws Exception {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws SQLException,
					HibernateException {
				Query q = session.createQuery(queryString);
				if(paramList != null){
					int size = paramList.size();
					for (int i = 0; i < size; i++) {
						q.setParameter(i, paramList.get(i));
					}
				}
				q.setFirstResult(firstRow);
				q.setMaxResults(maxRow);
				return q.list();
			}
		});
//		return getHibernateTemplate().find(queryString);
	}
	
	public List find(final String queryString,List paramList) throws Exception{
		return getHibernateTemplate().find(queryString, paramList.toArray());
	}

}
